package design.patterns.factorymode.web;

import org.springframework.stereotype.Controller;
import design.patterns.factorymode.factory.CarFactoryStepOne;
import design.patterns.factorymode.api.CarFactoryInterface;

import java.io.IOException;

/**
 * @Description 测试工厂模式.
 * @author: wwchen
 * @DateTime: 2018-07-01 20:07
 */
@Controller
public class FectoryDemo {


    /**
     * 作用：将接口的实现和客户端的分离
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("=================V1_使用工厂类，使接口实现和客户端分离===================================");
        CarFactoryStepOne carFactoryStepOne = new CarFactoryStepOne();
        CarFactoryInterface bmw = carFactoryStepOne.getCar("BMW");
        bmw.manufacture();
        CarFactoryInterface benz = carFactoryStepOne.getCar("Benz");
        benz.manufacture();

        System.out.println("");
        System.out.println("==================V2_利用反射，根据类名来指定要生产的对象方法================================");
        //根据类型生产对象
        CarFactoryInterface benzByClass = carFactoryStepOne.getCarByClass("designPatterns.factorymode.service.Benz");
        benzByClass.manufacture();
        CarFactoryInterface bmwByClass = carFactoryStepOne.getCarByClass("designPatterns.factorymode.service.BMW");
        bmwByClass.manufacture();

        System.out.println("");
        System.out.println("==================V3_利用反射，通过读取配置文件匹配传入的key进行生产================================");
        CarFactoryInterface benzByClassKey;
        try {
            benzByClassKey = carFactoryStepOne.getCarByClassKey("Benz");
            benzByClassKey.manufacture();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
